package admin

import (
	"config-client-v1/model"
	local_oss "config-client-v1/utils/local-oss"
	"config-client-v1/utils/tools"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
)

type AlertController struct {
}

func (con AlertController) List(ctx *gin.Context) {
	userToken := GetUserToken(ctx)
	list := make([]*model.AlertType, 0)
	err := model.DB.Where("project_id = ?", userToken.ProjectID).Find(&list).Error
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    6,
			"message": err.Error(),
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code":    0,
		"message": "success",
		"data": map[string]interface{}{
			"list": list,
		},
	})
}

func (con AlertController) Save(ctx *gin.Context) {
	userToken := GetUserToken(ctx)
	param := struct {
		ID    int    `json:"id" binding:"required"`
		Audio string `json:"audio" binding:"max=255"`
		Desc  string `json:"desc" binding:"max=255"`
	}{}
	err := ctx.ShouldBindBodyWith(&param, binding.JSON)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    2,
			"message": "无效的参数",
			"data":    err.Error(),
		})
		return
	}
	data := &model.AlertType{
		Audio: "",
		Desc:  param.Desc,
	}
	now := tools.GetNow()
	old := &model.AlertType{}
	err = model.DB.Where("id = ? AND project_id = ?", param.ID, userToken.ProjectID).First(old).Error
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    6,
			"message": err.Error(),
		})
		return
	}

	newPath := ""
	if param.Audio != "" {
		fileExist, err := local_oss.ObjectExist(param.Audio)
		if !fileExist {
			ctx.JSON(200, gin.H{
				"code":    2,
				"message": "文件不存在",
				"data":    err,
			})
			return
		}
		newPath, err = local_oss.ObjectMove(param.Audio, fmt.Sprintf("upload/audio/%d/", userToken.ProjectID))
		if err != nil {
			ctx.JSON(200, gin.H{
				"code":    7,
				"message": err.Error(),
			})
			return
		}
	}

	data.Audio = newPath
	data.UpdateTime = &now
	err = model.DB.Model(&model.AlertType{}).Where("id = ?", param.ID).Select("audio", "desc", "update_time").Updates(data).Error
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    6,
			"message": err.Error(),
		})
		return
	}

	ctx.JSON(200, gin.H{
		"code":    0,
		"message": "success",
		"data":    map[string]interface{}{},
	})
}

func (con AlertController) Delete(ctx *gin.Context) {
	param := struct {
		ID int `json:"id" binding:"required"`
	}{}
	ctx.JSON(200, gin.H{
		"code":    1,
		"message": "报警删除功能修改中",
		"data":    map[string]interface{}{},
	})
	return
	err := ctx.ShouldBindBodyWith(&param, binding.JSON)
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    2,
			"message": "无效的参数",
			"data":    err.Error(),
		})
		return
	}
	if param.ID <= 100 {
		ctx.JSON(200, gin.H{
			"code":    2,
			"message": "无效的参数 id ~",
			"data":    "",
		})
		return
	}
	err = model.DB.Where("id = ?", param.ID).Delete(&model.AlertType{}).Error
	if err != nil {
		ctx.JSON(200, gin.H{
			"code":    6,
			"message": err.Error(),
		})
		return
	}
	ctx.JSON(200, gin.H{
		"code":    0,
		"message": "success",
		"data":    map[string]interface{}{},
	})
}
